using System;
using MainGame.Menu;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace MainGame.GamePlay
{

  public class RecapRenderer : UIRenderer
  {
    Texture2D background, yes, no, backgroundLegnoso;
    bool fourPlayers;
    SpriteFont baveuseFont;
    Button OkButton, continueButton;
    private bool intro = true;

    //public float elapsedTime { get; set; }
    public RecapRenderer(Game game)
      : base(game)
    {
      //UpdateOrder = (int)ComponentOrders.UI;     
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(IUIRenderer));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      base.Initialize();

      OkButton = new Button(Game, @"Images//Menu//button2", new Rectangle(210, 400, 144, 55), Color.Red, "small_font", Locale.Strings.Back, Color.Yellow);
      continueButton = new Button(Game, @"Images//Menu//button2", new Rectangle(490, 400, 144, 55), Color.Blue, "small_font", Locale.Strings.Continue, Color.Yellow);
      
      if (gameState.Players.Count > 2)
        fourPlayers = true;

      if (fourPlayers)
        background = Game.Content.Load<Texture2D>(@"Images\\Menu\\SquarePaper4");
      else
        background = Game.Content.Load<Texture2D>(@"Images\\Menu\\SquarePaper");

      backgroundLegnoso = Game.Content.Load<Texture2D>(@"Images\\Menu\\sfondoGames");
     
      Outro = false;
    }

    protected override void LoadContent()
    {
      large_font = Game.Content.Load<SpriteFont>(@"large_font");
      baveuseFont = Game.Content.Load<SpriteFont>(@"baveuse1_font");      

      yes = Game.Content.Load<Texture2D>(@"Images\\Menu\\ok");
      no = Game.Content.Load<Texture2D>(@"Images\\Menu\\no");

      //carte = Game.Content.Load<Texture2D>(@"Images\\Menu\\LungoCarte");
      //denari = Game.Content.Load<Texture2D>(@"Images\\Menu\\Denari");
      //settebello = Game.Content.Load<Texture2D>(@"Images\\Menu\\SettebelloAlone");
      //primiera = Game.Content.Load<Texture2D>(@"Images\\Menu\\Settanta3D");
      //scope = Game.Content.Load<Texture2D>(@"Images\\Menu\\Scope");

      base.LoadContent();
    }

   
    public override void Update(GameTime gameTime)
    {
      base.Update(gameTime);
    }

    private int timer = 0;
    public override void UpdateRecap(GameTime gametime)
    {
      var boardRenderer = Game.Services.GetService(typeof(IBoardRenderer)) as BoardRenderer;

      if (((Game1)Game).IsBackButtonPressed())
      {
        OkButton.color = Color.Red;
        //if (OnMatchExit != null)
        //  OkButton();
      }

      foreach (var g in cachedGestures)
      {
        switch (g.GestureType)
        {
          case GestureType.Tap:
            // Check if menu button was tapped
            if (OkButton.IsInBounds(g.Position.ToPoint(screen_transform)))
            {
              OkButton.color = Color.Red;
              
                SaveData.SaveUtils.DeleteGame(Game);
                Game.GetContext().currentConfig.hasSavedGame = false;
                StateManagement.StateManager.StartMenu(Game);              
            }

            if (continueButton.IsInBounds(g.Position.ToPoint(screen_transform)))
            {
              continueButton.color = Color.Red;

              gameState.StartPlayerIndex++;
              Game.GetContext().PlayerTurn = gameState.StartPlayerIndex % gameState.Players.Count;

              StateManagement.StateManager.ContinueLevel(Game);
            }
            break;
        }
      }

      if (intro)
      {
        int speed = 20;
        rect.Width += speed;
        rect.Height += speed;

        if(rect.Width == 700) intro = false;
      }

      if (Game.GetContext().GameFinished)
      {
        timer++;

        if (timer > 20)
        {
          if (colorWinner == Color.Blue) colorWinner = Color.Red;
          else colorWinner = Color.Blue;
        }
      }
    }
  

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);
    }

    Rectangle rect = new Rectangle(400, 240, 500, 180);

    public override void DrawScore(GameTime gameTime)
    {
      // Draw a grey curtain
      spriteBatch.Draw(backgroundLegnoso, new Rectangle(0, 0, 800, 480), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.6f);
      //spriteBatch.Draw(whitePx, gameBounds, null, Color.Lerp(Color.Gray, Color.Transparent, 0.3f), 0.0f, Vector2.Zero, SpriteEffects.None, 0.7f);
      spriteBatch.Draw(background, rect, null, Color.White, 0.0f, new Vector2(400, 240), SpriteEffects.None, 0.85f);

      if (!intro)
      {
        // Titolo
        //spriteBatch.DrawString(font, Locale.Strings.Score, new Vector2(60, 50), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
        
        // Intestazioni
        spriteBatch.DrawString(baveuseFont, Locale.Strings.Score, new Vector2(290, 50), Color.Black, 0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 1.0f);
        spriteBatch.DrawString(baveuseFont, Locale.Strings.Wins, new Vector2(500, 50), Color.Black, 0.0f, Vector2.Zero, 0.7f, SpriteEffects.None, 1.0f);

        for (int i = 0; i < gameState.Players.Count; i++)
        {
          DrawPoints(i, 130 + (70 * i));
        }

        // Draw menu button
        spriteBatch.Draw(OkButton.texture, OkButton.bounds, null, OkButton.color, 0, Vector2.Zero, SpriteEffects.None, 0.99f);
        spriteBatch.DrawString(OkButton.spriteFont, OkButton.message, new Vector2((float)OkButton.bounds.Center.X, (float)OkButton.bounds.Center.Y), OkButton.textColor, 0, OkButton.spriteFont.MeasureString(OkButton.message) / 2, OkButton.scale, SpriteEffects.None, 1.0f);

        spriteBatch.Draw(continueButton.texture, continueButton.bounds, null, continueButton.color, 0, Vector2.Zero, SpriteEffects.None, 0.99f);
        spriteBatch.DrawString(continueButton.spriteFont, continueButton.message, new Vector2((float)continueButton.bounds.Center.X, (float)continueButton.bounds.Center.Y), continueButton.textColor, 0, continueButton.spriteFont.MeasureString(continueButton.message) / 2, continueButton.scale, SpriteEffects.None, 1.0f);

      }
    }

    private Color colorWinner = Color.Blue;
    private void DrawPoints(int p, int y)
    {

      String mess = gameState.Players[p].Name;
      

      int x = 300;

      if (fourPlayers)
      {

        int strLen = (int)baveuseFont.MeasureString(Locale.Strings.Team).X;
        
        if (p % 2 == 0)
        {
          y = 130;
          // Draw Name
          spriteBatch.DrawString(large_font, mess, new Vector2(70, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

          if (p != 0)
            spriteBatch.DrawString(large_font, gameState.Players[0].Wins.ToString(), new Vector2(x + 200, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
          else
          {
            //spriteBatch.Draw(box, new Rectangle(x + 202 + strLen, y + 2 + (p * 23), 50, 50), null, new Color(100, 100, 100, 100), 0.0f, Vector2.Zero, SpriteEffects.None, 0.9f);
            spriteBatch.Draw(box, new Rectangle(x + 194 + strLen, y + (p * 23), 50, 50), null, Color.DodgerBlue, 0.0f, Vector2.Zero, SpriteEffects.None, 0.89f);
            spriteBatch.DrawString(large_font, "" + 1, new Vector2(x + 204 + strLen, y + (p * 23)), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
            spriteBatch.DrawString(large_font, Locale.Strings.Team, new Vector2(x + 203, y + 3 + (p * 23)), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
            spriteBatch.DrawString(large_font, Locale.Strings.Team, new Vector2(x + 200, y + (p * 23)), Color.DodgerBlue, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
          }
          spriteBatch.DrawString(large_font, gameState.Players[p].Scores.ToString(), new Vector2(x + 30, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f); 
        }

        if (p % 2 == 1)
        { 
          y = 237; 
          spriteBatch.DrawString(large_font, mess, new Vector2(70, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

          if (p != 1)
            spriteBatch.DrawString(large_font, gameState.Players[1].Wins.ToString(), new Vector2(x + 200, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
          else
          {
            //spriteBatch.Draw(box, new Rectangle(x + 202 + strLen, y + 2 + (p * 23), 50, 50), null, new Color(100,100,100,100), 0.0f, Vector2.Zero, SpriteEffects.None, 0.89f);
            spriteBatch.Draw(box, new Rectangle(x + 194 + strLen, y + (p * 23), 50, 50), null, Color.OrangeRed, 0.0f, Vector2.Zero, SpriteEffects.None, 0.89f);
            spriteBatch.DrawString(large_font, "" + 2, new Vector2(x + 204 + strLen, y + (p * 23)), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
            spriteBatch.DrawString(large_font, Locale.Strings.Team, new Vector2(x + 203, y + 3 + (p * 23)), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.9f);
            spriteBatch.DrawString(large_font, Locale.Strings.Team, new Vector2(x + 200, y + (p * 23)), Color.OrangeRed, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
          }        

          spriteBatch.DrawString(large_font, gameState.Players[p].Scores.ToString(), new Vector2(x + 30, y + (p * 23)), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
        }

        }

      if (!fourPlayers)
      {
        spriteBatch.DrawString(large_font, mess, new Vector2(70, y), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
        spriteBatch.DrawString(large_font, gameState.Players[p].Wins.ToString(), new Vector2(x + 200, y), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
        spriteBatch.DrawString(large_font, gameState.Players[p].Scores.ToString(), new Vector2(x + 30, y), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
      }
    }

    
    public override event OnExit OnExit;
  }
}
